import 'package:cesi/model/Coffee.dart';
import 'package:cesi/model/shop.dart';
import 'package:cesi/theme/color.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class CartPage extends StatelessWidget {
  const CartPage({super.key});
  // 修改 removeFromCart 方法，接收 index 参数
  void removeFromCart(int index, BuildContext context) {
    // 获取 Shop 实例
    final shop = context.read<Shop>();
    // 获取要删除的咖啡对象
    final coffeeToRemove = shop.cart[index];
    // 调用 Shop 类中的 removeFromCart 方法
    shop.removeFromCart(coffeeToRemove);
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Shop>(
      builder: (context, value, child) => Scaffold(
        backgroundColor: AppColors.primaryColor,
        appBar: AppBar(
          elevation: 0,
          title: const Center(
            child: Text("购物车", style: TextStyle(color: Colors.white)),
          ),
          backgroundColor: AppColors.primaryColor,
          iconTheme: const IconThemeData(color: Colors.white),
        ),
        body: Column(
          children: [
            Expanded(
              child: ListView.builder(
                padding: const EdgeInsets.all(8.0), // 添加一些内边距
                itemCount: value.cart.length,
                itemBuilder: (context, index) {
                  // get food from cart
                  final Coffee coffee = value.cart[index];
                  // return list tile
                  return Container(
                    decoration: BoxDecoration(
                      color: AppColors.secondaryColor,
                      borderRadius: BorderRadius.circular(8),
                    ),
                    margin: const EdgeInsets.only(
                      left: 20,
                      right: 20,
                      top: 10,
                      bottom: 10,
                    ),
                    child: ListTile(
                      title: Text(
                        coffee.name,
                        style: const TextStyle(
                          color: Colors.white,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      subtitle: Text(
                        '¥${coffee.price}', // 添加货币符号更清晰
                        style: TextStyle(
                          color: Colors.grey[200],
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      // 2. 修改删除按钮的逻辑，使其能获取到正确的 index
                      trailing: IconButton(
                        icon: Icon(Icons.delete, color: Colors.grey[300]),
                        onPressed: () {
                          // 调用修改后的 removeFromCart 方法，并传入 index 和 context
                          removeFromCart(index, context);
                        },
                      ),
                    ),
                  );
                },
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  // 显示总价
                  Text(
                    '总计: ¥${calculateTotal(value.cart)}',
                    style: const TextStyle(
                      color: Colors.white,
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  // 结算按钮
                  ElevatedButton(
                    onPressed: () {
                      // 这里添加结算逻辑，例如跳转到支付页面或显示结算成功提示
                      ScaffoldMessenger.of(context).showSnackBar(
                        const SnackBar(content: Text('结算功能开发中...')),
                      );
                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.orange,
                      foregroundColor: Colors.white,
                      padding: const EdgeInsets.symmetric(
                        horizontal: 30,
                        vertical: 15,
                      ),
                      textStyle: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    child: const Text('去结算'),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 计算购物车总价
  double calculateTotal(List<Coffee> cart) {
    return cart.fold(0, (total, coffee) => total + double.parse(coffee.price));
  }
}
